// 格式化时间
/**
 * 时间类-->创建时间，格式化时间
 * @param date
 * @param fmt
 */
export class DateUtil {
  private date: Date
  private fmt: string
  public constructor(date: Date, fmt: string) {
    this.date = date ? date : new Date()
    this.fmt = fmt ? fmt : 'yyyy-MM-dd'
  }
  public getDate(): Date {
    return this.date
  }
  public setDate(date: Date): void {
    this.date = date
  }
  public getFmt(): string {
    return this.fmt
  }
  public setFmt(fmt: string): void {
    this.fmt = fmt
  }
  public format = (): string => {
    let _date = this.getDate()
    let fmt = this.getFmt()
    let o: any = {
      'M+': _date.getMonth() + 1, //月份
      'd+': _date.getDate(), //日
      'h+': _date.getHours(), //小时
      'm+': _date.getMinutes(), //分
      's+': _date.getSeconds(), //秒
      'q+': Math.floor((_date.getMonth() + 3) / 3), //季度
      S: _date.getMilliseconds() //毫秒
    }

    if (/(y+)/.test(fmt)) {
      fmt = fmt.replace(
        RegExp.$1,
        (_date.getFullYear() + '').substr(4 - RegExp.$1.length)
      )
    }
    for (let k in o) {
      if (new RegExp('(' + k + ')').test(fmt)) {
        fmt = fmt.replace(
          RegExp.$1,
          RegExp.$1.length == 1
            ? o[k]
            : ('00' + o[k]).substr(('' + o[k]).length)
        )
      }
    }
    return fmt
  }
}

// 格式化时间
export const formatDate = (date: Date) => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  return `${year}-${month}-${day}`
}

// 将数据转换成el-option数据
export const formatOptionData = (data: any[]) => {
  const result: any[] = []
  data.forEach((item: any) => {
    result.push({
      label: item.name,
      value: item.id
    })
  })
  return result
}
